package com.hljx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hljx.vo.SysDictVO;
import com.hljx.config.Result;
import com.hljx.dto.SysDictDTO;
import com.hljx.dto.SysDictPageDTO;
import com.hljx.service.SysDictService;
import com.hljx.vo.TargetSysDictVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 字典 前端控制器
 * </p>
 *
 * @author gx
 * @since 2023-09-18
 */
@RestController
@RequestMapping("/sysDict")
@Tag(name = "字典管理")
public class SysDictController {

    @Autowired
    SysDictService dictService;

    @Operation(summary = "字典管理-新增或编辑")
    @PostMapping("/saveOrUpdateDict")
    public Result<Boolean> saveOrUpdateDict(@RequestBody @Valid SysDictDTO sysDictDTO) {
        return Result.success(dictService.saveOrUpdateDict(sysDictDTO));
    }

    @Operation(summary = "字典管理-删除")
    @GetMapping("/deleteDict")
    public Result<Boolean> deleteDict(@RequestParam("id") Long id) {
        return Result.success(dictService.deleteDict(id));
    }

    @Operation(summary ="字典管理-修改启用状态:0禁止1启用")
    @GetMapping("/updateDictStatus")
    public Result<Boolean> updateDictStatus(@RequestParam("id") Long id,
                                            @RequestParam("status") Integer status) {
        return Result.success(dictService.updateDictStatus(id,status));
    }

    @Operation(summary = "字典管理-分页查询")
    @PostMapping("/listSysDictByPage")
    public Result<IPage<SysDictVO>> listSysDictByPage(@RequestBody SysDictPageDTO sysDictPageDTO) {
        return Result.success(dictService.listSysDictByPage(sysDictPageDTO));
    }


    @Operation(summary = "指标管理，科室下拉框")
    @GetMapping("/sysDictPull")
    public Result sysDictPull(){
        List<TargetSysDictVO> targetSysDictVOS = dictService.sysDictPull();
        return Result.success(targetSysDictVOS);
    }

    @Operation(summary = "导出和下载模板")
    @PostMapping("/export")
    public void export(@RequestBody SysDictPageDTO dto, HttpServletResponse response) throws ClassNotFoundException {
        dictService.export(dto,response);
    }

    @Operation(summary = "导入")
    @PostMapping("/importExcel")
    public void importExcel(@RequestParam("file") MultipartFile file, HttpServletResponse response) throws ClassNotFoundException, IOException {
        dictService.importExcel(file,response);
    }


    @Operation(summary = "指标管理-项目统计-来源")
    @GetMapping("/listProjectTargetAdministrationPull")
    public Result listProjectTargetAdministrationPull(String code){
        List<SysDictVO> sysDictVOS = dictService.listProjectTargetAdministrationPull(code);
        return Result.success(sysDictVOS);
    }
}
